package com.cxf.windex.biz;

import com.cxf.windex.base.biz.BaseBiz;
import com.cxf.windex.base.util.FetchUtil;
import com.cxf.windex.entity.CityInfo;
import com.cxf.windex.entity.JsBills;
import com.cxf.windex.mapper.CityInfoMapper;
import com.cxf.windex.mapper.JsBillsMapper;
import com.github.pagehelper.PageHelper;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @program: a_master_repair_master
 * @description:
 * @author: chenxf
 * @create: 2022-02-18 10:39
 **/

@Service
public class CityInfoBiz extends BaseBiz<CityInfoMapper, CityInfo> {
    FetchUtil fetchUtil=new FetchUtil();

    public void fetch(){
        try {
            List<CityInfo> cityInfos=fetchUtil.fetchProvince();
            cityInfos.forEach(e->{
                insertSelective(e);
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
        fetchDetail();

    }
    @Async
    public void fetchDetail(){
        while (fetchOne()){
            System.out.println("采集中");
        }
    }

    public boolean  fetchOne(){
        Example example = new Example(CityInfo.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("status", "0");
        //criteria.andLessThanOrEqualTo("crtTime", DateUtil.getByLocalDate(LocalDate.now()));
        example.setOrderByClause("id asc");
        PageHelper.startPage(1, 1);
        List<CityInfo> items = selectByExample(example);
        if(items.size()>0){
            CityInfo  cityInfo=items.get(0);
            try {
                List<CityInfo> cityInfos=fetchUtil.fetchCity(cityInfo.getFetchUrl(),cityInfo.getAreaLevel()+1);
                cityInfos.forEach(e->{
                    e.setParentId(cityInfo.getId());
                    insertSelective(e);
                });
                cityInfo.setStatus(1);
                updateSelectiveById(cityInfo);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return true;

        }
        return false;
    }

}
